﻿Public Class ADDAREA
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS
    Private Sub ADDAREA_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMaschangwat()
        SelectDatagridMasAmpur()
        SearchCHANGWATTxt()
    End Sub
    Private Sub SelectDatagridMaschangwat()
        Dim objSelectDataMASCHANGWAT As New MASCHANGWAT
        Dim MASCHANGWAT As DataTable
        MASCHANGWAT = connect.GetTable(objSelectDataMASCHANGWAT.SelectMaschangwat_)
        CHANGWATGrid.PrimaryGrid.DataSource = MASCHANGWAT
    End Sub
    Private Sub SelectDatagridMasAmpur()
        Dim objSelectDataMasampur As New MASAMPUR
        Dim MASAMPUR As DataTable
        MASAMPUR = connect.GetTable(objSelectDataMasampur.SelectMasampur_)
        AMPURGrid.PrimaryGrid.DataSource = MASAMPUR
    End Sub
    Private Sub SearchCHANGWATTxt()
        Dim sql2 As String
        sql2 = "SELECT CAST(idmaschangwat AS CHAR(10)) AS `idmaschangwat`,`changwat` FROM maschangwat WHERE `STATUS` = 1 "
        s = New FILTERCLASS(CHANGWATTxt2, sql2, "รหัสจังหวัด,จังหวัด", "100,200", "1,1", "1,1")
        s.SetShowBorder = True
    End Sub
    Private Sub AddChangwat_Click(sender As Object, e As EventArgs) Handles AddChangwat.Click
        If CHANGWATTxt.Text.ToString.Length > 0 Then
            Dim objInsertData As New MASCHANGWAT
            objInsertData.CHANGWATNAME_ = CHANGWATTxt.Text
            objInsertData.insertMaschangwat()

            SelectDatagridMaschangwat()

            cleardata()

            s.SetSql = "SELECT CAST(idmaschangwat AS CHAR(10)) AS `idmaschangwat`,`changwat` FROM maschangwat WHERE `STATUS` = 1 "
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub
    Private Sub EditChangwat_Click(sender As Object, e As EventArgs) Handles EditChangwat.Click
        If CHANGWATIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASCHANGWAT
                objeditData.CHANGWATID_ = CHANGWATIDTxt.Text
                objeditData.CHANGWATNAME_ = CHANGWATTxt.Text
                objeditData.editMaschangwat()

                SelectDatagridMaschangwat()

                cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddChangwat.Enabled = True
    End Sub
    Private Sub cleardata()
        CHANGWATIDTxt.Text = ""
        CHANGWATTxt.Text = ""
        SelectDatagridMaschangwat()
        SelectDatagridMasAmpur()
        AMPURIDTxt.Text = ""
        AMPURTxt.Text = ""
        CHANGWATTxt2.Text = ""
    End Sub
    Private Sub CHANGWATGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles CHANGWATGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            CHANGWATIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            CHANGWATIDTxt.Text = ""
        End If
        If grid.Cells("จังหวัด").Value() IsNot DBNull.Value Then
            CHANGWATTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            CHANGWATTxt.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE maschangwat SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `idmaschangwat` = '" & CHANGWATIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddChangwat.Enabled = False
    End Sub

    Private Sub AddAmpur_Click(sender As Object, e As EventArgs) Handles AddAmpur.Click
        If AMPURTxt.Text.ToString.Length > 0 Then
            Dim objInsertData As New MASAMPUR
            objInsertData.AMPURNAME_ = AMPURTxt.Text
            objInsertData.CHANGWATID_ = Convert.ToString(CHANGWATTxt2.Tag).Trim
            objInsertData.AMPURNAME_ = AMPURTxt.Text
            objInsertData.insertMasampur()

            SelectDatagridMasAmpur()

            cleardata()
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub EditAmpur_Click(sender As Object, e As EventArgs) Handles EditAmpur.Click
        If AMPURIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASAMPUR
                objeditData.CHANGWATID_ = Convert.ToString(CHANGWATIDTxt.Tag).Trim
                objeditData.AMPURID_ = AMPURIDTxt.Text
                objeditData.AMPURNAME_ = AMPURTxt.Text
                objeditData.editMasampur()

                SelectDatagridMasAmpur()

                cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddChangwat.Enabled = True
    End Sub

    Private Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles GroupBox2.Enter

    End Sub

    Private Sub AMPURGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles AMPURGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            AMPURIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            AMPURIDTxt.Text = ""
        End If
        If grid.Cells("อำเภอ").Value() IsNot DBNull.Value Then
            AMPURTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            AMPURTxt.Text = ""
        End If
        If grid.Cells("idmaschangwat").Value() IsNot DBNull.Value Then
            CHANGWATTxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("idmaschangwat").Value()
        Else
            CHANGWATTxt2.Tag = ""
        End If
        If grid.Cells("จังหวัด").Value() IsNot DBNull.Value Then
            CHANGWATTxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            CHANGWATTxt2.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE cpm.masampur SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `idmasampur` = '" & AMPURIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddAmpur.Enabled = False
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        cleardata()
    End Sub
End Class